﻿#if UNITY_ANDROID
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace Notifications.Runtime.Android
{
    
    public class AndroidBackgroundNotificationManager : IGameBackgroundNotificationsPlatform
    {
        private const string PACKAGE_NAME = "com.unity.notificationbackground";
        public const string PACKAGE_BACKGROUND_NAME = PACKAGE_NAME + ".IBackgroundNotification";
        private AndroidJavaObject _androidJavaObject;

        private bool _isForeground;
        private Dictionary<int, AndroidBackgroundNotification> _scheduledNotifications = new Dictionary<int, AndroidBackgroundNotification>();

        private  Dictionary<int, AndroidBackgroundNotification> _waitRemoves = new  Dictionary<int, AndroidBackgroundNotification>();
        
        public AndroidBackgroundNotificationManager()
        {
            _isForeground = true;
        }

        public void Init()
        {
            AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
            AndroidJavaClass managerClass = new AndroidJavaClass($"{PACKAGE_NAME}.AndroidBackgroundNotificationManager");
            _androidJavaObject = managerClass.CallStatic<AndroidJavaObject>("NotificationManagerImpl",  activity);
        }

        public void ScheduleBackgroundNotification(IGameBackgroundNotification gameNotification)
        {
            if (!_scheduledNotifications.TryGetValue(gameNotification.GetId(),out AndroidBackgroundNotification result))
            {
                result = gameNotification as AndroidBackgroundNotification;
                if (result!= null)
                {
                    _scheduledNotifications.Add(result.GetId(),result);
                    UpdateDateBackgroundNotification(gameNotification);
                    // _androidJavaObject.Call("ScheduleBackgroundNotification",result);
                }
            }
        }

        public void UpdateDateBackgroundNotification(IGameBackgroundNotification gameNotification)
        {
        }

        public void CancelBackgroundNotification(int id)
        {
            if (_scheduledNotifications.TryGetValue(id,out AndroidBackgroundNotification result))
            {
                result.NeedClose = true;
                _scheduledNotifications.Remove(id);
                if (_isForeground)
                {
                    _androidJavaObject.Call("CancelScheduleBackgroundNotification",result);
                }
                else
                {
                    _waitRemoves[id] = result;
                }
            }
        }

        public void CancelAllBackgroundNotifications()
        {
            if (_isForeground)
            {
                foreach (var result in _scheduledNotifications.Values)
                {
                    result.NeedClose = true;
                    _androidJavaObject.Call("CancelScheduleBackgroundNotification",result);
                }
                _scheduledNotifications.Clear();
            }
            else
            {
                foreach (var result in _scheduledNotifications)
                {
                    _waitRemoves[result.Key] = result.Value;
                }
            }
        }

        public void OnForeground()
        {
            _isForeground = true;
            foreach (var result in _scheduledNotifications.Values)
            {
                _androidJavaObject.Call("CancelScheduleBackgroundNotification",result);
            }
            foreach (var iWaitRemove in _waitRemoves)
            {
                CancelBackgroundNotification(iWaitRemove.Key);
            }
            _waitRemoves.Clear();
        }

        
        public void OnBackground()
        {
            _isForeground = false;
            LauncherAll();
        }

        private void LauncherAll()
        {
            foreach (var result in _scheduledNotifications.Values)
            {
                _androidJavaObject.Call("ScheduleBackgroundNotification",result);
            }
        }

        public IGameBackgroundNotification CreateBackgroundNotification()
        {
            return new AndroidBackgroundNotification();
        }

        public void Dispose()
        {
         
            foreach (var result in _scheduledNotifications.Values)
            {
                result.NeedClose = true;
                _androidJavaObject.Call("CancelScheduleBackgroundNotification",result);
            }
            _scheduledNotifications.Clear();   
            _androidJavaObject.Dispose();
            _waitRemoves.Clear();
        }
    }
}
#endif